#include <string.h>
#include "BC26103.h"
#include "stdio.h"
#include "tim.h"
#include "usart.h"
#include "main.h"
#include "TelecomPlatform.h"
extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];
uint8_t BC26_QLWSERV(uint8_t *TelecomPlatformIP,uint8_t*TelecomPlatformPort)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	const uint8_t Buf2[3]={'"',',',0};
	printf("\n--->>>Config IP and Port...\r\n");
	sprintf(ATStrBuf,"AT+QLWSERV=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,TelecomPlatformIP);
	strcat(ATStrBuf,Buf2);
	strcat(ATStrBuf,TelecomPlatformPort);
	strcat(ATStrBuf,"\r\n\0");
	printf("ATStrBuf=%s\n",ATStrBuf);
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,"AT+QLWSERV=",strlen("AT+QLWSERV="))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("BC26_QLWSERV success\r\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("BC26_QLWSERV fail\r\n");
	return FALSE;
}
uint8_t BC26_QLWCONF(uint8_t *IMEI)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	printf("\n--->>>Config Equipment IMEI\r\n");
	sprintf(ATStrBuf,"AT+QLWCONF=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,IMEI);
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,"\r\n\0");
	printf("ATStrBuf=%s\n",ATStrBuf);
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,"AT+QLWCONF=",strlen("AT+QLWCONF="))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF success\r\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("BC26_QLWCONF fail\r\n");
	return FALSE;
}
uint8_t BC26_QLWADDOBJ(void)
{
	uint8_t i=0,sockedid=0,result=0;
	uint8_t  ATStrBuf_UPchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','0',',','1',',','"','0','"','\r','\n',0};
	uint8_t  ATStrBuf_Downchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','1',',','1',',','"','0','"','\r','\n',0};
	printf("\n--->>>add LwM2M obj\r\n");
	i=3;
	while(i)
	{
	    --i;
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF up success\r\n");
			break;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	if(i==0)
	{
		printf("BC26_QLWCONF up fail\n");
	}
	i=3;
	while(i)
	{
		--i;
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("BC26_QLWCONF down success\r\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("BC26_QLWCONF down fail\r\n");
	return  FALSE;
}
uint8_t BC26_QLWOPEN(void)
{
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','O','P','E','N','=','1','\r','\n',0};
	printf("\n--->>>Send Registration request\r\n");
	i=2;
	while(i)
	{
		--i;
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(500);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		if((strncmp(UART1_Rx_Buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("BC26 Regist  success\n");
			if(strstr(UART1_Rx_Buf,"CONNECT OK")==NULL)
			{
				j=0;
				UART1_Rx_cnt=0 ;
				while(1)
				{
					HAL_Delay(1000);
					printf("*");
					if(++j>128)
					{
						break;
					}
					if(UART1_Rx_cnt>0)
					{
						HAL_Delay(1000);
						printf("len=%d\r\n",UART1_Rx_cnt);
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						printf("%s\n",UART1_Rx_Buf);
						if(strstr(UART1_Rx_Buf,"CONNECT OK")!=NULL)
						{
								break;
						}
						else if(strstr(UART1_Rx_Buf,"CONNECT FAIL")!=NULL)
						{
								return  FALSE;
						}
						else
						{
							UART1_Rx_cnt=0 ;
						}
					}
				}
			}
			j=0;
			UART1_Rx_cnt=0 ;
			while(1)
			{
				HAL_Delay(1000);
				printf("*");
				if(++j>60)
				{
					break;
				}
				if(UART1_Rx_cnt>0)
				{
					HAL_Delay(1000);
					printf("--len=%d\r\n",UART1_Rx_cnt);
					UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
					printf("%s\n",UART1_Rx_Buf);
					if(strstr(UART1_Rx_Buf,"+QLWOBSERVE:")!=NULL)
					{
						return TRUE;
					}
					else
					{
						UART1_Rx_cnt=0 ;
					}
				}
			}
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("BC26 Regist fail\n");
	return  FALSE;
}
uint8_t BC26_QLWUPDATE(void)
{
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','U','P','D','A','T','E','\r','\n',0};
	printf("\n--->>>Send update request\n");
	i=2;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(500);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		if((strncmp(UART1_Rx_Buf,"AT+QLWUPDATE",strlen("AT+QLWUPDATE"))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("QLWUPDATE success\n");
			if(strstr(UART1_Rx_Buf,"UPDATE OK")==NULL)
			{
				j=0;
				UART1_Rx_cnt=0 ;
				while(1)
				{
					HAL_Delay(1000);
					printf("*");
					if(++j>60)
					{
						break;
					}
					if(UART1_Rx_cnt>0)
					{
						HAL_Delay(1000);
						printf("len=%d\r\n",UART1_Rx_cnt);
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						printf("%s\n",UART1_Rx_Buf);
						if(strstr(UART1_Rx_Buf,"UPDATE OK")!=NULL)
						{
							printf("update success\n");
							return TRUE;
						}
						else	if(strstr(UART1_Rx_Buf,"UPDATE FAIL")!=NULL)
						{
							printf("update fail\r\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				printf("Update Success\r\n");
				return TRUE;
			}
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("BC26_QLWUPDATE fail\r\n");
	return FALSE;
}
uint8_t BC26_QLWCFG(void)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t  ATStrBuf[28+1]={'A','T','+','Q','L','W','C','F','G','=','"','d','a',\
	't','a','f','o','r','m','a','t','"',',','0',',','0','\r','\n',0};
	printf("\n--->>>Config dataformat\r\n");
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("AT+QLWCFGsuccess\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("AT+QLWCFG fail\r\n");
	return FALSE;
}
uint16_t SendDataToTelecomPlatform(uint8_t*buffer,uint16_t len)
{
    uint8_t i=3,j=0;
    uint8_t ATStrBuf[150]={0};
    sprintf(ATStrBuf,"AT+QLWDATASEND=19,0,0,%d,%s,0x0100\r\n",len,buffer);
		printf("%s\r\n",ATStrBuf);
    while(i--)
    {
        UART1_Rx_cnt=0;
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf)+1);
        HAL_Delay(500);
        printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        if((strncmp(UART1_Rx_Buf,"AT+QLWDATASEND=",strlen("AT+QLWDATASEND="))==0)&&\
        (strstr(UART1_Rx_Buf,"OK")!=NULL))
        {
        	printf("send  success\r\n");
			if(strstr(UART1_Rx_Buf,"SEND OK")==NULL)
			{
				UART1_Rx_cnt=0 ;
				j=30;
				while(j--)
				{
					printf("*");
					HAL_Delay(1000);
					if(UART1_Rx_cnt>0)
					{
						printf("len=%d\r\n",UART1_Rx_cnt);
						UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
						printf("%s\n",UART1_Rx_Buf);
						if(strstr(UART1_Rx_Buf,"SEND OK")!=NULL)
						{
							 printf("send  data success\r\n");
							 return	TRUE;
						}
						else if(strstr(UART1_Rx_Buf,"SEND FAIL")!=NULL)
						{
							printf("send  data fail\r\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				printf("Send  Data Success\r\n");
				return	TRUE;
			}
        }
        else
        {
            printf("Send  ERR\r\n");
			HAL_Delay(2000);
        }
    }
    return FALSE ;
}
#if 0
uint16_t BC26_QLWDATASTATUS(void)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t  ATStrBuf[19+1]={'A','T','+','Q','L','W','D','A','T','A','S','T',\
	'A','T','U','S','?','\r','\n',0};
	printf("\n--->>>Enquiry send status\r\n");
	i=128;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
            result=UtilGetIntFlag(UART1_Rx_Buf,"+QLWDATASTATUS:","\r\n");
            printf("status=%d\n",result);
            if(result==4)
            {
            	 printf("send data success\n");
				return TRUE;
            }
			else if((result==5)||(result==3)||(result==2))
			{
				printf("send data fail\r\n");
				return FALSE;
			}
			else if(result==1)
			{
				printf("wait\r\n");
			}
		}
	}
	printf("AT+QLWCFG fail\r\n");
	return FALSE;
}
#endif
uint16_t	ReceiveMessageFromTelecomPlatform(uint8_t*recebuffer)
{
	uint8_t i=0,sockedid=0,len=0 ;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','R','D','=','1','0','0','\r','\n',0};
	uint8_t *start;
	uint8_t k=0;
	printf("\n--->>>Receive Data from Telecom\r\n");
	i=10;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		printf("len=%d\n",UART1_Rx_cnt);
		if((strncmp(UART1_Rx_Buf,"AT+QLWRD=100",strlen("AT+QLWRD=100"))==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			start = strstr(UART1_Rx_Buf, "+QLWRD:");
			if (start != NULL)
			{
				if(UART1_Rx_cnt>=14)
				{
					for(k=0;k<UART1_Rx_cnt-7;k++)
					{
						if( (UART1_Rx_Buf[k]=='W') && (UART1_Rx_Buf[k+1]=='R') && (UART1_Rx_Buf[k+2]=='D') && (UART1_Rx_Buf[k+3]==':') && (UART1_Rx_Buf[k+4]==' ') && (UART1_Rx_Buf[k+7]==',') )
						{
							len = ((UART1_Rx_Buf[k+5]-'0')<<3) + ((UART1_Rx_Buf[k+5]-'0')<<1) + (UART1_Rx_Buf[k+6]-'0');
							break;
						}
					}
				}
				UART1_Rx_Buf[UART1_Rx_cnt] = 0;
				printf("Rev data:%s\r\n",UART1_Rx_Buf);
				if(len>0)
				{
					printf("len=%d\r\n",len);
					start = strstr(UART1_Rx_Buf, ",");
					if (start != NULL)
					{
						start = strstr(start, "\r\n");
						if (start != NULL)
						{
							memcpy(recebuffer,start+strlen("\r\n"),len);
							recebuffer[len]=0;
							printf("data=%s\r\n",recebuffer);
							return len;
						}
					}
				}
			}
		}
	}
	printf("NO data to receive\r\n");
	return 0;
}
uint8_t BC26_QLWDEL(void)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t  ATStrBuf[11+1]={'A','T','+','Q','L','W','D','E','L','\r','\n'};
	printf("\n--->>>ɾ�� LwM2M ����\r\n");
	i=3;
	while(i--)
	{
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(2000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		printf("%s\n",UART1_Rx_Buf);
		if((strncmp(UART1_Rx_Buf,ATStrBuf,strlen(ATStrBuf)-2)==0)&&\
		(strstr(UART1_Rx_Buf,"OK")!=NULL))
		{
			printf("AT+QLWDEL success\r\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	printf("AT+QLWDEL fail\r\n");
	return FALSE;
}